package org.lakeside.outreach.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class ProjectListPanel extends Composite implements ClickHandler {

	private WindowPanel window = new WindowPanel("Project List");
	private HorizontalPanel controls = new HorizontalPanel();
	private Button addProjectButton = new Button("New Project");
	private AddProjectPopupPanel addProjectPanel = new AddProjectPopupPanel();
	private FlexTable listTable = new FlexTable();
	
	private List<Project> projectsList = null;
	
	private int startIndex, selectedRow = -1;
	
	public ProjectListPanel() {
		// construct Controls panel
		controls.add(addProjectButton);
		controls.setStyleName("controls");
		
		// construct table
		listTable.setText(0, 0, "Name");
		listTable.setText(0, 1, "Description");
		listTable.addClickHandler(this);
		
		loadProjects();		
		
		window.add(controls);
		window.add(listTable);
		
		window.setWidth("100%");
		
		initWidget(window);
		
		addProjectButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				addProjectPanel.show();
			}
			
		});
	}
	
	private void updateList(List<Project> projects) {
		listTable.clear();
		projectsList = projects;
		int row = 0;
		System.out.println("Displaying projects");
		for(Project project: projects) {
			final int currentRow = row;
			listTable.setWidget(row, 0, new HTML("<b>"+project.getName()+"</b>"));
			listTable.setText(row, 1, project.getDescription());
			listTable.setWidget(row, 2, new Button("x"));
			((Button)listTable.getWidget(row, 2)).addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					delete(currentRow);
				}
				
			});
			row++;
		}
	}
	
	public void delete(int row) {
		Project toBeDeleted = projectsList.get(row);
		System.out.println(toBeDeleted.getName());
		
		ProjectServiceAsync ps = GWT.create(ProjectService.class);
		ps.removeProject(toBeDeleted, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Void result) {
				loadProjects();
			}
			
		});
	}
	
	public void loadProjects() {
		ProjectServiceAsync ps = GWT.create(ProjectService.class);
		ps.getProjects(new AsyncCallback<List<Project>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<Project> result) {
				updateList(result);
			}
		});
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();
		if (sender == listTable) {
			// selected the row that was clicked (-1 to account for header row).
			Cell cell = listTable.getCellForEvent(event);
			if (cell != null) {
				int row = cell.getRowIndex();
				if (row > -1) {
					selectRow(row);
				}
			}
		}
	}
	
	private void selectRow(int row) {
		// when a row other than the first row (the header) is clicked,
		// display its associated Project
		Project project = projectsList.get(startIndex + row);
		if(project == null) {
			return;
		}
		
		styleRow(selectedRow, false);
		styleRow(row, true);
		
		selectedRow = row;
		OutreachManager.get().getProjectDetailPanel().setProject(project);
	}
	
	private void styleRow(int row, boolean selected) {
		if(row != -1) {
			if(selected) {
				listTable.getRowFormatter().addStyleName(row, "project-SelectedRow");
			} else {
				listTable.getRowFormatter().removeStyleName(row, "project-SelectedRow");
			}
		}
	}
}
